<?php
/**
 * 首页控制器
 *
 * @author: yanxs <541139655@qq.com>
 * @version: 1.0
 * @date: 2021/3/8
 */

namespace App\Http\Controllers\Backend;
use App\Exceptions\CommonException;
use App\Services\Exception;
use App\Services\Helper;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    //protected $cloudUrl = "http://60.30.17.19:808/K3Cloud/";
    public function __construct()
    {
        //$this->
    }

    /**
     * 项目全局入口
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function index()
    {
        try {
            $userInfo =  $this->getUserInfo();
            //$userInfo['group_id'] = 2;
            $menuList = $this->menuServices()->getSidebar($userInfo['group_id']);
            //Helper::EasyThrowException("20001",__FILE__.__LINE__);
            return view('backend.index' ,['menuList'=>$menuList]);
        } catch (CommonException $e) {
            abort(404);
        }

    }

    /**
     * 登录
     * @param Request $request
     * @return mixed
     */
    public function login(Request  $request)
    {
        return $this->view('common.login');
    }
    /**
     * 退出登录
     * @param Request $request
     * @return mixed
     */
    public function logOut(Request $request)
    {
        $request->session()->forget('admin');
        return redirect('/login');
    }

    public function indexbak(Request  $request)
    {
        try {
           // $cloudUrl = "http://60.30.17.19:808/K3Cloud";


            $res = $this->kindLogin();
            //业务逻辑,在底层业务逻辑也可抛出异常.控制器层统一接收
            $username = $request->input("username");
            if ($username == '') {
                app(Exception::class)->throwException("10001",__FILE__.__LINE__);
            }

            return $this->jsonSuccess([]);
        } catch (CommonException $exception) {
            return $this->jsonFailed($exception->getCode(), $exception->getMessage());
            var_dump($exception->getMessage());
        }

    }


    protected function kindLogin()
    {
        $data = [
            '60082b4e622f66',//帐套Id
            'Administrator',//用户名
            '888888',//密码
            2052//语言标识
        ];

        $loginurl = $this->cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc';
        $cookie_jar = tempnam('d:/data/tmp','CloudSession');
        $post_content = $this->create_postdata($data);
        return $this->invoke_post($loginurl,$post_content,$cookie_jar,TRUE);

    }


    protected function invoke_post($url,$post_content,$cookie_jar,$isLogin)
    {

        $ch = curl_init($url);
        $this_header = array(
            'Content-Type: application/json',
            'Content-Length: '.strlen($post_content)
        );

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if($isLogin){
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        }
        else{
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

    //构造Web API请求格式
    protected function create_postdata($args) {
        $postdata = array(
            'format'=>1,
            'useragent'=>'ApiClient',
            'rid'=>$this->create_guid(),
            'parameters'=>$args,
            'timestamp'=>date('Y-m-d'),
            'v'=>'1.0'
        );

        return json_encode($postdata);
    }

    protected function create_guid() {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12)
            .chr(125);// "}"
        return $uuid;
    }
}
